package com.efuture.androidmvvmdemo.listener

import android.content.Intent
import com.efuture.androidmvvmdemo.view.ui.LoginActivity
import com.efuture.androidmvvmdemo.view.ui.RegisterActivity
import com.efuture.androidmvvmdemo.viewmodel.RegisterViewModel
import com.efuture.netrequestdemo.bean.User

/**

 *@description:注册面事件处理
 * @author: zhaoshanshan
 * @date: 2022/3/27 2:28 下午

 */
class RegisterListener(val registerViewModel:RegisterViewModel,val activity: RegisterActivity){

    //注册用户
    fun registerUser(userName:String,passWord:String){

        if(userName.isEmpty()){
            ToastUtils.showShort("用户名不能为空")
            return
        }
        if(passWord.isEmpty()){
            ToastUtils.showShort("密码不能为空")
            return
        }
        registerViewModel.registerUser(userName,passWord)

    }

    //处理注册业务
    fun dealRegisterResult(user: User?) {
        if(null!=user&&user.errorCode==0){//成功
            activity.startActivity(Intent(activity, LoginActivity::class.java))
        }else{//失败
            if(user==null){
                ToastUtils.showShort("注册失败,返回用户为空")
                return
            }
            if(null!=user.errorMsg){
                ToastUtils.showShort(user.errorMsg)
            }else{
                ToastUtils.showShort("注册失败")
            }
        }
    }

}